<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:pkg="http://expath.org/ns/pkg"
                xmlns:web="http://expath.org/ns/webapp"
                xmlns:app="http://example.org/ns/webapp/gae/datastore"
                xmlns:gae="http://fgeorges.org/ns/gae/tools"
                xmlns="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="#all"
                version="2.0">

   <xsl:import href="http://fgeorges.org/ns/gae/tools.xsl"/>

   <pkg:import-uri>http://example.org/ns/webapp/gae/datastore.xsl</pkg:import-uri>

   <xsl:variable name="uri-xslt"   select="'urn:x-example.org:name:xslt'"/>
   <xsl:variable name="uri-xquery" select="'urn:x-example.org:name:xquery'"/>
   <xsl:variable name="uri-xproc"  select="'urn:x-example.org:name:xproc'"/>

   <xsl:function name="app:home">
      <xsl:param name="request" as="element(web:request)"/>
      <xsl:param name="bodies"  as="item()*"/>
      <web:response status="200" message="Ok">
         <web:body content-type="application/xml" method="xhtml"/>
      </web:response>
      <html>
         <head>
            <title>Datastore content</title>
         </head>
         <body>
            <p>Here are the values in the datastore for the following languages:</p>
            <dl>
               <dt>XSLT:</dt>
               <dd>
                  <xsl:value-of select="gae:load($uri-xslt)"/>
               </dd>
               <dt>XQuery:</dt>
               <dd>
                  <xsl:value-of select="gae:load($uri-xquery)"/>
               </dd>
               <dt>XProc:</dt>
               <dd>
                  <xsl:value-of select="gae:load($uri-xproc)"/>
               </dd>
            </dl>
            <p>To change any of these values, use one of the following forms.  Each of them does
              the same thing, that is, saving an XML document with the value of the field, using
              gae:save(), but each of them using a different technology (resp. XSLT, XQuery and
              XProc) and using its own datastore slot (that is, XSLT does not override nor even
              see the value saved by say, XProc).</p>
            <p>
               <form action="xslt" method="get">
                  <span>Value: </span>
                  <input type="text" name="value"/>
                  <input type="submit" value="XSLT"/>
               </form>
            </p>
            <p>
               <form action="xquery" method="get">
                  <span>Value: </span>
                  <input type="text" name="value"/>
                  <input type="submit" value="XQuery"/>
               </form>
            </p>
            <p>
               <form action="xproc" method="get">
                  <span>Value: </span>
                  <input type="text" name="value"/>
                  <input type="submit" value="XProc"/>
               </form>
            </p>
         </body>
      </html>
   </xsl:function>

   <xsl:function name="app:save-xslt">
      <xsl:param name="request" as="element(web:request)"/>
      <xsl:param name="bodies"  as="item()*"/>
      <xsl:variable name="value" select="$request/web:param[@name eq 'value']/@value"/>
      <web:response status="200" message="Ok">
         <web:body content-type="application/xml" method="xhtml"/>
      </web:response>
      <html>
         <head>
            <title>Save value using XSLT</title>
         </head>
         <body>
            <p>
               <xsl:text>Received value: </xsl:text>
               <xsl:value-of select="$value"/>
               <xsl:text> (going to save it...)</xsl:text>
            </p>
            <xsl:variable name="doc">
               <value xmlns="">
                  <xsl:value-of select="$value"/>
               </value>
            </xsl:variable>
            <xsl:choose>
               <xsl:when test="gae:save($uri-xslt, $doc)">
                  <p>Successful!</p>
               </xsl:when>
               <xsl:otherwise>
                  <p>Something went wrong...</p>
               </xsl:otherwise>
            </xsl:choose>
         </body>
      </html>
   </xsl:function>

</xsl:stylesheet>
